package cn.edu.ctbu.shiyan07.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Component
@Aspect
@Slf4j
public class HttpAspect {
    @Pointcut("execution(* cn.edu.ctbu.shiyan07.webapi.StudentRestController.*(..))")
    public void log(){
    }
    @Before("log()")
    public void dobefore(){
        System.out.println("准备执行getbypage...");
    }
    @After("log()")
    public void doafter(){
        System.out.println("getbypage执行完毕，即将返回...");
    }
    @AfterReturning(returning = "object",pointcut = "log()")
    public void AfterR(Object object){
        log.info("response={}",object);
    }
    @Around("log()")
    public Object aronud(ProceedingJoinPoint pjp) throws Throwable{
        StopWatch stopWatch=new StopWatch();
        stopWatch.start();
        Object obj =pjp.proceed(pjp.getArgs());
        stopWatch.stop();
        long cost =stopWatch.getTotalTimeMillis();
        MethodSignature signature=(MethodSignature) pjp.getSignature();
        String methoName =signature.getDeclaringTypeName()+"."+signature.getName();
        log.info("-------执行"+methoName+"方法，用时："+cost+"ms--------");
        return obj;
    }
}
